package com.cup.bddm.api.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.cup.bddm.api.res.Msg;
import com.cup.bddm.api.res.ResVo;
import com.cup.bddm.api.service.UserLogService;
import com.cup.bddm.config.Config;
import com.cup.bddm.impala.po.User;
import com.cup.bddm.util.CacheUtil;
import com.cup.bddm.util.JsonUtil;

public class BaseController {
	static Logger logger = Logger.getLogger(UserLogController.class);

	public static final String REQ_USER = "REQ_USER"; // 当前用户
	public static final String REQ_TIME = "REQ_TIME"; // 请求开始时间
	public static final String REQ_BODY = "REQ_PARAMS"; // 请求参数
	public static final String RES_BODY = "RES_BODY"; // 响应结果

	@Autowired
	public HttpServletRequest req;

	@Autowired
	public UserLogService userLogService;

	public static Msg the() {
		return new Msg();
	}

//	public boolean invalidToken(ReqVo vo) {
//		String token = vo.getToken();
//		if (token == null || !Pattern.matches("^[0-9a-zA-Z]{32}$", token)) {
//			token_err = "token错误";
//			return true;
//		}
//		Date date = CacheUtil.times.get(token);
//		User user = CacheUtil.users.get(token);
//		if (date == null || user == null) {
//			token_err = "token无效";
//			return true;
//		}
//		if (new Date().getTime() - date.getTime() > CacheUtil.token_pass_time) {
//			token_err = "token已过期";
//			return true;
//		}
//		return false;
//	}

	public User user(User user) {
		req.setAttribute(REQ_USER, user);
		return user;
	}

	public User user() {
		return (User) req.getAttribute(REQ_USER);
	}

	public String tokenStatus() {
		String token = req.getHeader("token");
		if (token == null || !Pattern.matches("^[0-9a-zA-Z]{32}$", token)) {
			return "token错误";
		}
		// 开发模式下可使用测试token
		if (Config.getBoolean("dev", false) && token.equals(Config.get("dev.token"))) {
			User user = new User();
			user.setCup_branch_ins_id_cd(Config.CUP_INS_ID);
			user.setCup_branch_nm("dev_admin");
			user.setLogin_name("dev_admin");
			user.setUser_id("dev_admin");
			user.setSub_ins_nm("dev_admin");
			user.setRole_id("1");
			user(user);
			logger.info("Uncheck token, This request is dev_admin. maybe PostMan.");
			return "";
		}
		Date date = CacheUtil.times.get(token);
		if (date == null || user(CacheUtil.users.get(token)) == null) {
			return "token无效";
		}
		if (new Date().getTime() - date.getTime() > CacheUtil.token_pass_time) {
			CacheUtil.delToken(token);
			return "token已过期";
		}
		CacheUtil.delayToken(token);
		return "";
	}

	public void writeRes(HttpServletRequest req, HttpServletResponse res, String msg) {
		res.setCharacterEncoding("UTF-8");
		res.setContentType("application/json; charset=UTF-8");
		try {
			String json = JsonUtil.toJson(ResVo.err(msg));
			req.setAttribute(RES_BODY, json);
			PrintWriter w = res.getWriter();
			w.write(json);
			w.flush();
			w.close();
		} catch (IOException e) {
			logger.error(e);
		}
	}
}
